package com.chance.cc.crawler.development.bootstrap.weibo;

import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/1/28 16:50
 * @Description
 *      微博api订阅
 **/
public class WeiboApiCorn {
    private static final String domainId = "weibo";

    private static final String access_token = "2.00o4_w1HrAaeYBedf38e38b8SnITmD";
    private static final String app_key = "1428199813";

    private static final String subscribeUrl = "https://c.api.weibo.com/subscribe/update_subscribe.json";
    private static final String subscribeSite = "apiCornSubscribe";

    private static final String subscribeDataUrl = "https://c.api.weibo.com/commercial/push?source=1428199813&access_token=2.00o4_w1HrAaeYBedf38e38b8SnITmD&subid=12054";
    private static final String subscribeDataSite = "apiCornSubscribeData";

    private static final String limitedDataUrl = "https://c.api.weibo.com/2/search/statuses/limited.json";
    private static final String limitedDataSite = "apiCornLimitedData";

    public static void main(String[] args) {
//        subscribeRecord();
        subscribeDataRecord();
//        limitedDataRecord();

//        String decode = null;
//        try {
//            decode = URLEncoder.encode("小公主", "UTF-8");
//            URLEncoder.encode()
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        System.out.println(decode);
//        URLEncodedUtils.parse("https://c.api.weibo.com/2/search/statuses/limited.json?access_token=2.00o4_w1HrAaeYBedf38e38b8SnITmD&q=小公主&count=12&endtime=1609518600000&source=1428199813&starttime=1609432200000&page=1", Charset.defaultCharset()).
    }

    private static void limitedDataRecord(){
//        CrawlerRequestRecord requestRecord = requestRecord(limitedDataUrl, limitedDataSite);
//        requestRecord.getHttpRequest().setMethod(HttpConstant.Method.GET);
//        CrawlerRequestRecord keywordRecord = keywordRecord(limitedDataSite);
//        controller(requestRecord,keywordRecord);
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\Download\\weibo_limited.txt")), "UTF-8"));

            String line = "";
            if((line = bufferedReader.readLine()) != null){
                Map map = JSONObject.parseObject(line).toJavaObject(Map.class);
                Set set = map.keySet();
                String url = limitedDataUrl + "?";
                for (Object o : set) {
                    String key = o.toString();
                    url = url + key + "=" + map.get(key) + "&";
                }
                url = url.substring(0,url.length() -1);

                CrawlerRequestRecord build = CrawlerRequestRecord.builder()
                        .startPageRequest(domainId, turnPage)
                        .httpUrl(url)
                        .releaseTime(System.currentTimeMillis())
                        .needWashed(true)
                        .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                        .build();

                build.tagsCreator().bizTags().addSite(limitedDataSite);

                DevCrawlerController.builder()
                        .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                        .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domainId))
                        .consoleResultPipeline()
                        .requestRecord(build)
                        .build().start();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }


    private static void subscribeDataRecord(){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(subscribeDataUrl)
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .build();
        requestRecord.tagsCreator().bizTags().addSite(subscribeDataSite);

        DevCrawlerController.builder()
                .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domainId))
                .consoleResultPipeline()
                .requestRecord(requestRecord)
                .build().start();
    }

    private static void subscribeRecord(){
        CrawlerRequestRecord requestRecord = requestRecord(subscribeUrl, subscribeSite);
        requestRecord.getHttpRequest().addExtra("method",HttpConstant.Method.POST);
        CrawlerRequestRecord keywordRecord = keywordRecord(subscribeSite);
        controller(requestRecord,keywordRecord);
    }

    private static CrawlerRequestRecord requestRecord(String url,String site){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(url)
                .needParsed(false)
                .needWashed(false)
                .releaseTime(System.currentTimeMillis())
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite(site);
        return requestRecord;
    }

    private static CrawlerRequestRecord keywordRecord(String site){
        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_search_kw",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domainId+"/keys?site=" + site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return keywordRecord;
    }

    private static void controller(CrawlerRequestRecord requestRecord,CrawlerRequestRecord keywordRecord){
        DevCrawlerController.builder()
                .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domainId))
                .consoleResultPipeline()
                .requestRecord(requestRecord)
                .supportRecord(keywordRecord)
                .build().start();
    }

}
